<template>
  <div style="width: 100%">
    <div id="deawer" style="padding:4px">
      <v-tableTitle :btnConfig="btnConfig" @cancel="cancel" @save="save"></v-tableTitle>
      <div class="detailBox" style="height:300px">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import config from "../Config";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"
import tableTitle from "commons/TableTitle";

export default {
  name: "",
  mixins: [baseMixin, config],
  data() {
    return {
      flag: true,
      detailFlag: false,
      newFlag: false,
      differenceId:null
    };
  },
  created() {
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('common.save'), certigier: !this.btnPermNameList.includes('WM06020407'), invalid: "save" },
        { label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM06020408'), invalid: "cancel" },
      ]
    }
  },
  methods: {
    //保存按钮
    async save() {
      let flag = this.$refs.detail.$children[0].$refs.input.submitForm("from");

      if (!flag) {
        return;
      }
      if (this.$refs.detailList.$children[1].$refs.input) {
        let detailFlag = this.$refs.detailList.$children[1].$refs.input.submitForm("from");
        if (!detailFlag) {
          return;
        }
      }

      let detail = this.$refs.detail.detailData;
      let detailList;
      let serialList
      if (this.$refs.detailList) {
        detailList = this.$refs.detailList.detailData;
        serialList = this.$refs.detailList.serialList;
        this.detailFlag = this.$refs.detailList.detailsFlag;
      }
      if(serialList.length > 0&&serialList.some(item => !item.serialNumber)){
        return this.$message.error(this.$tj('in.serialNumberInfo','app.emptyMes'))
      }
      let data = {};
      if (this.newFlag) {
        data = {
          ...detail,
          whseid: this.$store.state.wms,
          adjustmentdetails: [{
            ...detailList,
            adjustmentkey: detail.adjustmentkey,
            storerkey: detail.storerkey,
            whseid: this.$store.state.wms,
            dataList:serialList
          }],
        }
        if(this.differenceId){
          data.differenceId = this.differenceId;
        }
      } else if (this.detailFlag) {
        data = {
          ...detail,
          adjustmentdetails: [{
            ...detailList,
            adjustmentkey: detail.adjustmentkey,
            storerkey: detail.storerkey,
            whseid: this.$store.state.wms
          }],
        }
        if(serialList.length > 0){
          const adjustmentLinenumber = serialList.find(item => item.adjustmentLinenumber).adjustmentLinenumber;
          const index = data.adjustmentdetails.findIndex(item => item.adjustmentlinenumber == adjustmentLinenumber);
          data.adjustmentdetails[index].dataList = serialList;
        }
      } else {
        data = {
          ...detail,
        }
      }
      console.log(serialList,data);
      try {
        let res = await this.$api.post(`/excuteAdjustment`, data);
        
          this.$message.success(res.returnMgr);
          if (this.newFlag) {
            this.cancel();
          } else if (this.detailFlag) {
            this.$refs.detailList.detailsFlag = false;
            this.$refs.detailList.detailsNew = false;
            this.$refs.detailList.detailNav = "1";
          } else {
            this.cancel();
          }
        
      } catch (e) {
        this.$message.error(e)
      }
    },
    //弹框取消
    cancel() {
      this.$router.push({ name: "adjustment" });
    },
  },
  mounted() {
    this.newFlag = this.$store.state.newFlag;
    console.log(this.newFlag);
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.detailsFlag = data.detailListFlag;
    }
    if(this.newFlag&&this.$store.state.routeData?.id){
      this.differenceId = this.$store.state.routeData.id;
    }
  },
  watch: {},
  components: {
    "v-detail": detail,
    "v-detailList": detailList,
    "v-tableTitle": tableTitle,
  }
}
</script>

<style scoped>

</style>
